package com.ydx.aa76111.agent;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.ydx.aa76111.agent.runtime.Agent;

import java.lang.instrument.Instrumentation;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * attachagent启动入口
 */
public class AgentMain {

    public static void agentmain(String options, Instrumentation instrumentation) throws Exception {
        PreMain.premain(options, instrumentation);
        dumpData();
    }

    private static void dumpData() {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
                .setPriority(60)
                .setNameFormat("agent-scheduled-dump-data-thread-pool")
                .build());
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                byte[] data = Agent.getInstance().getExecutionData(false);
            }
        });
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                byte[] data = Agent.getInstance().getExecutionData(false);
            }
        });
    }
}
